package cn.com.Service;

import cn.com.shiro.AbstractBaseRedisDao;
import org.springframework.data.redis.core.BoundHashOperations;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.concurrent.TimeUnit;

/**
 * Created by luokun on 2018/3/16.
 */
@Service
public class RedisSaveManagerServiceImpl extends AbstractBaseRedisDao<String, String> implements RedisSaveManageService {

    /**
     * 指定缓存失效时间
     * @param key 键
     * @param time 时间(秒)
     * @return
     */
    public boolean expire(String key,long time){
        try {
            if(time>0){
                redisTemplate.expire(key, time, TimeUnit.SECONDS);
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 普通缓存放入并设置时间
     * @param key 键
     * @param time 时间(秒) time要大于0 如果time小于等于0 将设置无限期
     * @return true成功 false 失败
     */
    @Override
    public  void setAccount(String key,Integer account, long time){
        try {
            if(time>0){
                redisTemplate.opsForValue().set(key, account.toString(), time, TimeUnit.SECONDS);
            }else{
                redisTemplate.opsForValue().set(key, account.toString());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void setString(String key,String s,long time) {
        try {
            if(time>0){
                redisTemplate.opsForValue().set(key, s, time, TimeUnit.SECONDS);
            }else{
                redisTemplate.opsForValue().set(key, s);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public String getString(String key) {
    return redisTemplate.opsForValue().get(key);
    }

    /**
     * 普通缓存获取
     * @param key 键
     * @return 值
     */
    @Override
    public Integer get(String key){
        String val;

        if(exists(key)) {
            val = redisTemplate.opsForValue().get(key);
        }else {
            return 0;
        }
        if(StringUtils.isEmpty(val)){
            return 0;
        }

        return Integer.parseInt(val);

    }

    /**
     * 删除map中的某个对象
     * @author lh
     * @date 2016年8月10日
     * @param key   map对应的key
     * @param field map中该对象的key
     */
    @Override
    public void delMapField(String key, String... field){
        BoundHashOperations<String, String, ?> boundHashOperations = redisTemplate.boundHashOps(key);
        boundHashOperations.delete(field);
    }
    /**
     * 向key对应的map中添加缓存对象
     * @param key   cache对象key
     * @param field map对应的key
     * @param obj   对象
     */
    @Override
    public  <T> void addMapField(String key, String field,String obj){
        redisTemplate.opsForHash().put(key, field, obj);

    }
    /**
     * 获取map缓存中的某个对象
     * @param key
     * @return
     */
    @Override
    public Object hget(String key,String item){
        return redisTemplate.opsForHash().get(key, item);
    }


    /**
     * 判断缓存是否存在
     * @param key
     * @return
     */
    public boolean exists( String key) {
       return redisTemplate.hasKey(key);
    }
    /**
     * 删除缓存
     * @param key 可以传一个值 或多个
     */
    @SuppressWarnings("unchecked")
    @Override
    public void del(String ... key){
        if(key!=null&&key.length>0){
            if(key.length==1){
                redisTemplate.delete(key[0]);
            }else{
                redisTemplate.delete(CollectionUtils.arrayToList(key));
            }
        }
    }
    /**
     * 自增
     * @param key 键
     * @return 值
     */
    @Override
    public void increment(String key,long time) {
      Integer account=  get( key)+1;
        setAccount(key,account,  time);
    }

}
